package com.xiaopeng.common.httpResult.model;


import com.xiaopeng.common.httpResult.enums.HttpResultCodeEnum;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一的公共响应体
 * @author xiaopeng
 */
@Data
@Accessors(chain = true)
public class HttpResult<T> implements Serializable {

    private static final long serialVersionUID = 5323132353231323123L;

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 描述信息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    private HttpResult() {
    }


    /**
     * 成功处理
     */
    public static <T> HttpResult<T> success() {
        HttpResult<T> httpResult = new HttpResult<T>();
        httpResult.setSuccess(true);
        httpResult.setCode(HttpResultCodeEnum.SUCCESS.getCode());
        httpResult.setMessage(HttpResultCodeEnum.SUCCESS.getMessage());
        return httpResult;
    }

    /**
     * 成功处理
     */
    public static <T> HttpResult<T> success(T data) {
        HttpResult<T> httpResult = new HttpResult<T>();
        httpResult.setSuccess(true);
        httpResult.setCode(HttpResultCodeEnum.SUCCESS.getCode());
        httpResult.setMessage(HttpResultCodeEnum.SUCCESS.getMessage());
        httpResult.setData(data);
        return httpResult;
    }

    /**
     * 失败处理
     */
    public static <T> HttpResult<T> fail(Integer code, String message) {
        HttpResult<T> httpResult = new HttpResult<T>();
        httpResult.setSuccess(false);
        httpResult.setCode(code);
        httpResult.setMessage(message);
        return httpResult;
    }

}
